import sys
from UI import Ui_Form
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox
from Sort_Methords import *
import random
import time

class Demo(QWidget, Ui_Form):
    def __init__(self):
        super(Demo, self).__init__()
        self.setupUi(self)
        self.combobox_init()
        self.button_init()
        self.dic = {
            1: lambda li: BubbleSort(li),
            2: lambda li: QuickSort(li, 0, len(li)-1),
            3: lambda li: MergeSort(li, 0, len(li)-1),
            4: lambda li: HeapSort(li),
            5: lambda li, max_count: count_sort(li, max_count),
            6: lambda li, max_num: BucketSort(li, 100, max_num),
            7: lambda li: ShellSort(li),
            8: lambda li: RadixSort(li)
        }

    def button_init(self):
        self.button_sort.clicked.connect(self.sort)
        self.button_reset.clicked.connect(self.reset)
        self.button_clear.clicked.connect(self.clear)

    def combobox_init(self):
        methords = ['--请选择--', '冒泡排序', '快速排序', '归并排序', '堆排序', '计数排序', '桶排序', '希尔排序', '基数排序']
        self.Methord_Sort.addItems(methords)

    def sort(self):
        min_num = int(self.Min_Num.text())
        max_num = int(self.Max_Num.text())
        li = [random.randint(min_num,max_num) for _ in range(eval(self.Num.text()))]
        choice = self.Methord_Sort.currentIndex()
        print(choice)
        if choice == 0:
            QMessageBox.information(self, 'warning', '请选择一种排序方法')
        elif choice == 5:
            start = time.time()
            print(self.dic[choice](li, max_num))
            end = time.time()
            cost = end - start
            self.Show_info.append(self.Methord_Sort.currentText()+'用时：\n'+str(cost)+'s')
        elif choice == 6:
            start = time.time()
            print(self.dic[choice](li, max_num))
            end = time.time()
            cost = end - start
            self.Show_info.append(self.Methord_Sort.currentText() + '用时：\n' + str(cost) + 's')
        else:
            start = time.time()
            print(self.dic[choice](li))
            end = time.time()
            cost = end - start
            self.Show_info.append(self.Methord_Sort.currentText() + '用时：\n' + str(cost) + 's')


    def reset(self):
        self.Max_Num.clear()
        self.Min_Num.clear()
        self.Num.clear()
        self.Methord_Sort.setCurrentIndex(0)

    def clear(self):
        self.Show_info.clear()



if __name__=='__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())

# def add(x):
#     return x+1
#
# dic={1:lambda x:add(x), 2: lambda li: BubbleSort(li)}
#
# print(dic[2]([3,1,2]))

